import logging
from colorlog import ColoredFormatter


class MyLogger:
    def __init__(self, name):
        logging.basicConfig(level=logging.INFO, encoding='utf-8')
        self.logger = logging.getLogger(name)
        # 创建控制台处理器
        handler = logging.StreamHandler()
        # 设置日志级别
        handler.setLevel(logging.DEBUG)
        # 创建带颜色的日志格式
        formatter = ColoredFormatter(
            "%(log_color)s%(asctime)s %(name)s %(levelname)s %(message)s",
            datefmt=None,
            reset=True,
            log_colors={
                'DEBUG': 'cyan',
                'INFO': 'green',
                'WARNING': 'yellow',
                'ERROR': 'red',
                'CRITICAL': 'bold_red',
            },
            secondary_log_colors={},
            style='%'
        )
        # 将格式应用到处理器
        handler.setFormatter(formatter)
        # 将处理器添加到记录器
        self.logger.addHandler(handler)
        # 禁用 Django 默认的日志处理器
        self.logger.propagate = False
